package com.ffcs.crmd.tsp.server.job.info;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import io.netty.channel.Channel;

/**
 * 
 * 功能说明:子任务转移轨迹
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class JobDetailTrack {
    
    /**
     * 死通道
     */
    private volatile Map<Channel, Map<Integer, JobDetailTrackInfo>> channelInfo = new HashMap<Channel, Map<Integer, JobDetailTrackInfo>>();
    
    /**
     * 最后一次运行的通道
     */
    private volatile Channel                                        lastchannel;
    
    /**
     * 最后一次发送运行的时间
     */
    private volatile Date                                           lastDate;
    
    /**
     * 最后第二次运行的通道
     */
    private volatile Channel                                        lastSecondchannel;
    
    /**
     * 最后第二次发送运行的时间
     */
    private volatile Date                                           lastSecondDate;
    
    /**
     * 
     * 功能说明:获取死通道列表
     * 
     * @return 死通道列表
     */
    public Map<Channel, Map<Integer, JobDetailTrackInfo>> getChannelInfo() {
        return channelInfo;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param channel
     * @param jobDetailShell
     */
    public void putChannel(Channel channel, JobDetailShell jobDetailShell) {
        this.lastSecondchannel = this.lastchannel;
        this.lastSecondDate = this.lastDate;
        this.lastchannel = channel;
        this.lastDate = jobDetailShell.getSendTime();
        Map<Integer, JobDetailTrackInfo> jobDetailTrackInfoMap = this.channelInfo.get(channel);
        if (MapUtils.isEmpty(jobDetailTrackInfoMap)) {
            jobDetailTrackInfoMap = new HashMap<Integer, JobDetailTrackInfo>();
        }
        
        JobDetailTrackInfo jobDetailTrackInfo = jobDetailTrackInfoMap.get(jobDetailShell.getRemotingOpaque());
        if(jobDetailTrackInfo == null){
            jobDetailTrackInfo = new JobDetailTrackInfo();
        }
        
        jobDetailTrackInfo.setFireTime(new Date(jobDetailShell.getFireTime().getTime()));
        jobDetailTrackInfo.setSendTime(new Date(jobDetailShell.getSendTime().getTime()));
        if(jobDetailShell.getReciveTime() != null){
            jobDetailTrackInfo.setReciveTime(new Date(jobDetailShell.getReciveTime().getTime()));
        }
        
        jobDetailTrackInfoMap.put(jobDetailShell.getRemotingOpaque(), jobDetailTrackInfo);
        this.channelInfo.put(channel, jobDetailTrackInfoMap);
    }
    
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param channel
     * @param jobDetailShell
     */
    public void updateReciveStatus(Channel channel, JobDetailShell jobDetailShell) {
        Map<Integer, JobDetailTrackInfo> jobDetailTrackInfoMap = this.channelInfo.get(channel);
        JobDetailTrackInfo jobDetailTrackInfo = jobDetailTrackInfoMap.get(jobDetailShell.getRemotingOpaque());
        jobDetailTrackInfo.setReciveTime(new Date(jobDetailShell.getReciveTime().getTime()));
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param channel
     * @param jobDetailShell
     */
    public void updateCompleteStatus(Channel channel, JobDetailShell jobDetailShell) {
        Map<Integer, JobDetailTrackInfo> jobDetailTrackInfoMap = this.channelInfo.get(channel);
        JobDetailTrackInfo jobDetailTrackInfo = jobDetailTrackInfoMap.get(jobDetailShell.getRemotingOpaque());
        jobDetailTrackInfo.setCompleteTime(new Date(jobDetailShell.getCompleteTime().getTime()));
    }
    
    
    /**
     * 功能说明: 获取最后一次运行的通道
     *
     * @return lastchannel 最后一次运行的通道
     */
    public Channel getLastchannel() {
        return lastchannel;
    }
    
    /**
     * 功能说明: 获取最后一次发送运行的时间
     *
     * @return lastDate 最后一次发送运行的时间
     */
    public Date getLastDate() {
        return lastDate;
    }

    /**
     * 功能说明: 获取最后第二次运行的通道
     *
     * @return lastSecondchannel 最后一次运行的通道
     */
    public Channel getLastSecondchannel() {
        return lastSecondchannel;
    }
    

    /**
     * 功能说明: 获取最后第二次发送运行的时间
     *
     * @return lastSecondDate 最后一次发送运行的时间
     */
    public Date getLastSecondDate() {
        return lastSecondDate;
    }
    
}
